'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: router.py
* @Time: 2025/11/5
* @All Rights Reserve By Brtc
'''
from dataclasses import dataclass

from aiohttp.client_exceptions import ssl_errors
from flask import Flask, Blueprint
from injector import inject

from internal.handler import AppHandler, app_handler, BuiltInHandler, ApiToolHandler, UploadFileHandler, DatasetHandler, \
    DocumentHandler, OAuthHandler
from internal.service import DatasetService


@inject
@dataclass
class Router:
    app_handler: AppHandler
    builtin_handler: BuiltInHandler
    api_tool_handler: ApiToolHandler
    upload_file_handler: UploadFileHandler
    dataset_handler: DatasetHandler
    document_handler: DocumentHandler
    oauth_handler: OAuthHandler

    """
    * @Author Leon-liao
    * @Function:路由注册函数
    * @Description //所有请求都要在这里注册
    * @Date :2025/11/5  14:31
    * @Param:
    """
    def register_router(self, app:Flask):
        blue_printer = Blueprint('llmops-ai-platform', __name__, url_prefix='')

        blue_printer.add_url_rule("/ping", methods=["GET", "OPTIONS"],view_func=self.app_handler.ping)
        blue_printer.add_url_rule("/app/chat_bot", methods=["POST"],view_func=self.app_handler.chat_bot)
        blue_printer.add_url_rule("/app", methods=["POST"], view_func=self.app_handler.create_app)
        blue_printer.add_url_rule("/app/<uuid:app_id>", view_func=self.app_handler.get_app)
        blue_printer.add_url_rule("/app/<uuid:app_id>", methods=["POST"], view_func=self.app_handler.update_app)
        blue_printer.add_url_rule("/app/<uuid:app_id>/delete", methods=["POST"], view_func=self.app_handler.delete_app)

        # 内置工具相关的 接口
        blue_printer.add_url_rule("/builtin-tools", view_func=self.builtin_handler.get_builtin_tools)
        blue_printer.add_url_rule("/builtin-tools/<string:provider_name>/tools/<string:tool_name>", view_func=self.builtin_handler.get_provider_tool)
        blue_printer.add_url_rule("/builtin-tools/<string:provider_name>/icon",view_func=self.builtin_handler.get_provider_icon)
        blue_printer.add_url_rule("/builtin-tools/categories",view_func=self.builtin_handler.get_categories)

        # 自定义工具
        blue_printer.add_url_rule("/api-tools/validate-openapi-schema", methods=["POST"],view_func=self.api_tool_handler.validate_openapi_schema)
        blue_printer.add_url_rule("/api-tools", methods=["POST"], view_func=self.api_tool_handler.create_api_tool_provider)
        blue_printer.add_url_rule("/api-tools",view_func=self.api_tool_handler.get_api_tool_providers_with_page)
        blue_printer.add_url_rule("/api-tools/<uuid:provider_id>", methods=["POST"], view_func=self.api_tool_handler.update_api_tool_provider)
        blue_printer.add_url_rule("/api-tools/<uuid:provider_id>", view_func=self.api_tool_handler.get_api_tool_provider, )
        blue_printer.add_url_rule("/api-tools/<uuid:provider_id>/tools/<string:tool_name>",view_func=self.api_tool_handler.get_api_tool, )
        blue_printer.add_url_rule("/api-tools/<uuid:provider_id>/delete", methods=["POST"],view_func=self.api_tool_handler.delete_api_tool_provider, )

        # 文件上传服务
        blue_printer.add_url_rule("/upload-files/file", methods=["POST"], view_func=self.upload_file_handler.upload_file)

        #知识库模块
        blue_printer.add_url_rule("/datasets", view_func=self.dataset_handler.get_datasets_with_page)
        blue_printer.add_url_rule("/datasets", methods=["POST"], view_func=self.dataset_handler.create_dataset)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>", view_func=self.dataset_handler.get_dataset)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>", methods=["POST"],view_func=self.dataset_handler.update_dataset)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/documents",view_func=self.document_handler.get_documents_with_page,)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/documents/<uuid:document_id>",view_func=self.document_handler.get_document,)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/documents/<uuid:document_id>/name",methods=["POST"],view_func=self.document_handler.update_document_name,)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/documents", methods=["POST"], view_func=self.document_handler.create_documents)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/documents/batch/<string:batch>", view_func=self.document_handler.get_documents_status)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/documents/<uuid:document_id>/enabled",methods=["POST"], view_func=self.document_handler.update_document_enabled)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/hit",methods=["POST"],view_func=self.dataset_handler.hit)
        blue_printer.add_url_rule("/datasets/<uuid:dataset_id>/queries", view_func=self.dataset_handler.get_dataset_queries)


        #授权认证模块
        blue_printer.add_url_rule("/oauth/<string:provider_name>", view_func=self.oauth_handler.provider)
        blue_printer.add_url_rule("/oauth/authorize/<string:provider_name>", methods = ["POST"],view_func=self.oauth_handler.authorize)

        app.register_blueprint(blue_printer)